package com.example.mybatisplus.web.controller;

import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.domain.Violations;
import com.example.mybatisplus.service.ViolationsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.AdminsService;
import com.example.mybatisplus.model.domain.Admins;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2024-07-16
 * @version v1.0
 */
@Controller
@RequestMapping("/api/admins")
public class AdminsController {
    @Autowired
    private AdminsService adminsService;

    @PostMapping("login")
    @ResponseBody
    public JsonResponse login(@RequestBody Admins admins){
        Admins admin = adminsService.login(admins);
        return JsonResponse.success(admin);
    }

    @GetMapping("exit")
    @ResponseBody
    public JsonResponse logout(){
        SessionUtils.clearCurUser();
        return JsonResponse.success(true);
    }

    @PostMapping("register")
    @ResponseBody
    public JsonResponse register(@RequestBody Admins admins){
        Admins admin = adminsService.register(admins);
        return JsonResponse.success(admin);
    }

    private final Logger logger = LoggerFactory.getLogger( ViolationsController.class );

    @GetMapping("/getAll")
    @ResponseBody
    public JsonResponse<List<Admins>> getAllViolations() {
        try {
            List<Admins> adminsList = adminsService.getAllAdmins();
            return JsonResponse.success(adminsList, "获取成功");
        } catch (Exception e) {
            logger.error("获取失败", e);
            return JsonResponse.message(false, 500, "获取失败");
        }
    }

  @PostMapping("/insert")
  @ResponseBody
    public JsonResponse insertAll(@RequestBody Admins admins) {
        try {
            adminsService.insertAll(admins);
        } catch (Exception e){
            return JsonResponse.failure("插入失败");
        }
      return JsonResponse.success(null, "插入成功");
  }

    @PostMapping("/update")
    @ResponseBody
    public JsonResponse update(@RequestBody Admins admins) {
        try {
            adminsService.update(admins);
        } catch (Exception e){
            return JsonResponse.failure("更新失败");
        }
        return JsonResponse.success(null, "更新成功");
    }

    @PostMapping("/delete")
    @ResponseBody
    public JsonResponse delete(@RequestBody Admins admins) {
        try {
            adminsService.delete(admins);
        } catch (Exception e){
            return JsonResponse.failure("删除失败");
        }
        return JsonResponse.success(null, "删除成功");
    }


    @PostMapping("/select")
    @ResponseBody
    public JsonResponse<List<Admins>> selectAdmins(@RequestBody Admins admins) {
        try {
            List<Admins> adminsList = adminsService.selectAdmins(admins);
            return JsonResponse.success(adminsList, "获取成功");
        } catch (Exception e) {
            logger.error("获取失败", e);
            return JsonResponse.message(false, 500, "获取失败");
        }
    }

}

